<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" slot="detail" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="事件类型" prop="eventType">
              <a-input v-model="model.eventType" placeholder="请输入事件类型"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="事件位置" prop="eventLocation">
              <a-input v-model="model.eventLocation" placeholder="请输入事件位置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="事件图片" prop="eventPicture">
              <j-image-upload v-model="model.eventPicture" isMultiple></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="事件发生时间"
                               prop="eventHappenTiem">
              <j-date v-model="model.eventHappenTiem" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择事件发生时间" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道号" prop="channelId">
              <a-input v-model="model.channelId" placeholder="请输入通道号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="事件地区" prop="eventArea">
              <a-input v-model="model.eventArea" placeholder="请输入事件地区"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="初审事件状态" prop="eventState">
              <j-dict-select-tag v-model="model.eventState" dictCode="event_state" placeholder="请选择初审事件状态"
                                 type="list"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="初审时间" prop="checkTime">
              <j-date v-model="model.checkTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" disabled
                      placeholder="请选择初审时间" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="初审人" prop="checkBy">
              <a-input v-model="model.checkBy" disabled placeholder="请输入初审人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="复审事件状态"
                               prop="eventStateRecheck">
              <j-dict-select-tag v-model="model.eventStateRecheck" dictCode="event_state_recheck" disabled placeholder="请选择复审事件状态"
                                 type="list"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="复审时间" prop="recheckTime">
              <j-date v-model="model.recheckTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" disabled
                      placeholder="请选择复审时间" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="复审人" prop="recheckBy">
              <a-input v-model="model.recheckBy" disabled placeholder="请输入复审人"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane :key="refKeys[0]" :forceRender="true" tab="监控事件数据子表">
        <j-vxe-table
          :ref="refKeys[0]"
          :columns="sSEventsDataResTable.columns"
          :dataSource="sSEventsDataResTable.dataSource"
          :disabled="formDisabled"
          :loading="sSEventsDataResTable.loading"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
          keep-source
        />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

import {JVxeTableModelMixin} from '@/mixins/JVxeTableModelMixin.js'
import {JVXETypes} from '@/components/jeecg/JVxeTable'
import {getRefPromise, VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import JFormContainer from '@/components/jeecg/JFormContainer'

export default {
  name: 'SSEventsDataForm',
  mixins: [JVxeTableModelMixin],
  components: {
    JFormContainer,
  },
  data() {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      model: {
        eventState: "0",
        eventStateRecheck: "0",
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {},
      refKeys: ['sSEventsDataRes',],
      tableKeys: ['sSEventsDataRes',],
      activeKey: 'sSEventsDataRes',
      // 监控事件数据子表
      sSEventsDataResTable: {
        loading: false,
        dataSource: [],
        columns: [
          // {
          //   title: '事件id',
          //   key: 'eventId',
          //   type: JVXETypes.input,
          //   width: "200px",
          //   placeholder: '请输入${title}',
          //   defaultValue: '',
          // },
          {
            title: '处理人',
            key: 'processedBy',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '初审状态',
            key: 'checkStatus',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            dataIndex: 'eventState_dictText'

          },
          {
            title: '复审状态',
            key: 'recheckStatus',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            dataIndex: 'eventStateRecheck_dictText'

          },
          {
            title: '审批时间',
            key: 'createTime',
            type: JVXETypes.datetime,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
        ]
      },
      url: {
        add: "/sSEventsData/sSEventsData/add",
        edit: "/sSEventsData/sSEventsData/edit",
        queryById: "/sSEventsData/sSEventsData/queryById",
        sSEventsDataRes: {
          list: '/sSEventsData/sSEventsData/querySSEventsDataResByMainId'
        },
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
  },
  methods: {
    addBefore() {
      this.sSEventsDataResTable.dataSource = []
    },
    getAllTable() {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      this.$nextTick(() => {
      })
      // 加载子表数据
      if (this.model.id) {
        let params = {id: this.model.id}
        this.requestSubTableData(this.url.sSEventsDataRes.list, params, this.sSEventsDataResTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm(allValues) {
      return new Promise((resolve, reject) => {
        Promise.all([]).then(() => {
          resolve(allValues)
        }).catch(e => {
          if (e.error === VALIDATE_FAILED) {
            // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
            this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
          } else {
            console.error(e)
          }
        })
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        sSEventsDataResList: allValues.tablesValue[0].tableData,
      }
    },
    validateError(msg) {
      this.$message.error(msg)
    },

  }
}
</script>

<style scoped>
</style>
